home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / X11R4 / cmds / X / os / sprite / io.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-19  |  19.7 KB  |  793 lines

  1. /***********************************************************
  2. Copyright 1987, 1989 by Digital Equipment Corporation, Maynard, Massachusetts,
  3. and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its 
  8. documentation for any purpose and without fee is hereby granted, 
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in 
  11. supporting documentation, and that the names of Digital or MIT not be
  12. used in advertising or publicity pertaining to distribution of the
  13. software without specific, written prior permission.  
  14.  
  15. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  16. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  17. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  18. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  19. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  20. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  21. SOFTWARE.
  22.  
  23. ******************************************************************/
  24. /* $XConsortium: io.c,v 1.65 89/09/14 16:19:50 rws Exp $ */
  25. /*****************************************************************
  26.  * i/o functions
  27.  *
  28.  *   WriteToClient, ReadRequestFromClient
  29.  *   InsertFakeRequest, ResetCurrentRequest
  30.  *
  31.  *****************************************************************/
  32.  
  33. #include <stdio.h>
  34. #include "Xos.h"
  35. #include "Xmd.h"
  36. #include <errno.h>
  37. #include <sys/param.h>
  38. #include <sys/uio.h>
  39. #include "X.h"
  40. #include "Xproto.h"
  41. #include "os.h"
  42. #include "osdep.h"
  43. #include "opaque.h"
  44. #include "dixstruct.h"
  45. #include "misc.h"
  46.  
  47. extern void MarkClientException();
  48. extern long ClientsWithInput[];
  49. extern long ClientsWriteBlocked[];
  50. extern long OutputPending[];
  51. extern long OutputBufferSize;
  52. extern int ConnectionTranslation[];
  53. extern Bool NewOutputPending;
  54. extern Bool AnyClientsWriteBlocked;
  55. static Bool CriticalOutputPending;
  56. static int timesThisConnection = 0;
  57. static ConnectionInputPtr FreeInputs = (ConnectionInputPtr)NULL;
  58. static ConnectionOutputPtr FreeOutputs = (ConnectionOutputPtr)NULL;
  59. static OsCommPtr AvailableInput = (OsCommPtr)NULL;
  60.  
  61. static ConnectionInputPtr AllocateInputBuffer();
  62. static ConnectionOutputPtr AllocateOutputBuffer();
  63.  
  64. extern int errno;
  65.  
  66. #define request_length(req, cli) ((cli->swapped ? \
  67.     lswaps((req)->length) : (req)->length) << 2)
  68. #define MAX_TIMES_PER         10
  69.  
  70. /*****************************************************************
  71.  * ReadRequestFromClient
  72.  *    Returns one request in client->requestBuffer.  Return status is:
  73.  *
  74.  *    > 0  if  successful, specifies length in bytes of the request
  75.  *    = 0  if  entire request is not yet available
  76.  *    < 0  if  client should be terminated
  77.  *
  78.  *    The request returned must be contiguous so that it can be
  79.  *    cast in the dispatcher to the correct request type.  Because requests
  80.  *    are variable length, ReadRequestFromClient() must look at the first 4
  81.  *    bytes of a request to determine the length (the request length is
  82.  *    always the 3rd and 4th bytes of the request).  
  83.  *
  84.  *    Note: in order to make the server scheduler (WaitForSomething())
  85.  *    "fair", the ClientsWithInput mask is used.  This mask tells which
  86.  *    clients have FULL requests left in their buffers.  Clients with
  87.  *    partial requests require a read.  Basically, client buffers
  88.  *    are drained before select() is called again.  But, we can't keep
  89.  *    reading from a client that is sending buckets of data (or has
  90.  *    a partial request) because others clients need to be scheduled.
  91.  *****************************************************************/
  92.  
  93. #define YieldControl()                \
  94.         { isItTimeToYield = TRUE;        \
  95.       timesThisConnection = 0; }
  96. #define YieldControlNoInput()            \
  97.         { YieldControl();            \
  98.       BITCLEAR(ClientsWithInput, fd); }
  99. #define YieldControlDeath()            \
  100.         { timesThisConnection = 0; }
  101.  
  102. int
  103. ReadRequestFromClient(client)
  104.     ClientPtr client;
  105. {
  106.     OsCommPtr oc = (OsCommPtr)client->osPrivate;
  107.     register ConnectionInputPtr oci = oc->input;
  108.     int fd = oc->fd;
  109.     int result, gotnow, needed;
  110.     register xReq *request;
  111.  
  112.     if (AvailableInput)
  113.     {
  114.     if (AvailableInput != oc)
  115.     {
  116.         AvailableInput->input->next = FreeInputs;
  117.         FreeInputs = AvailableInput->input;
  118.         AvailableInput->input = (ConnectionInputPtr)NULL;
  119.     }
  120.     AvailableInput = (OsCommPtr)NULL;
  121.     }
  122.     if (!oci)
  123.     {
  124.     if (oci = FreeInputs)
  125.     {
  126.         FreeInputs = oci->next;
  127.     }
  128.     else if (!(oci = AllocateInputBuffer()))
  129.     {
  130.         YieldControlDeath();
  131.         return -1;
  132.     }
  133.     oc->input = oci;
  134.     }
  135.     oci->bufptr += oci->lenLastReq;
  136.  
  137.     request = (xReq *)oci->bufptr;
  138.     gotnow = oci->bufcnt + oci->buffer - oci->bufptr;
  139.     if ((gotnow < sizeof(xReq)) ||
  140.     (gotnow < (needed = request_length(request, client))))
  141.     {
  142.     oci->lenLastReq = 0;
  143.     if ((gotnow < sizeof(xReq)) || (needed == 0))
  144.        needed = sizeof(xReq);
  145.     else if (needed > MAXBUFSIZE)
  146.     {
  147.         YieldControlDeath();
  148.         return -1;
  149.     }
  150.     if ((gotnow == 0) ||
  151.         ((oci->bufptr - oci->buffer + needed) > oci->size))
  152.     {
  153.         if ((gotnow > 0) && (oci->bufptr != oci->buffer))
  154.         bcopy(oci->bufptr, oci->buffer, gotnow);
  155.         if (needed > oci->size)
  156.         {
  157.         char *ibuf;
  158.  
  159.         ibuf = (char *)xrealloc(oci->buffer, needed);
  160.         if (!ibuf)
  161.         {
  162.             YieldControlDeath();
  163.             return -1;
  164.         }
  165.         oci->size = needed;
  166.         oci->buffer = ibuf;
  167.         }
  168.         oci->bufptr = oci->buffer;
  169.         oci->bufcnt = gotnow;
  170.     }
  171. #ifdef SPRITEPDEVCONN
  172.     if (PdevIsPdevConn(fd)) {
  173.         result = PdevRead(fd, oci->buffer + oci->bufcnt, 
  174.               oci->size - oci->bufcnt); 
  175.     } else
  176. #endif
  177.     result = read(fd, oci->buffer + oci->bufcnt, 
  178.               oci->size - oci->bufcnt); 
  179.     if (result <= 0)
  180.     {
  181.         if ((result < 0) && (errno == EWOULDBLOCK))
  182.         {
  183.         YieldControlNoInput();
  184.         return 0;
  185.         }
  186.         YieldControlDeath();
  187.         return -1;
  188.     }
  189.     oci->bufcnt += result;
  190.     gotnow += result;
  191.     /* free up some space after huge requests */
  192.     if ((oci->size > BUFWATERMARK) &&
  193.         (oci->bufcnt < BUFSIZE) && (needed < BUFSIZE))
  194.     {
  195.         char *ibuf;
  196.  
  197.         ibuf = (char *)xrealloc(oci->buffer, BUFSIZE);
  198.         if (ibuf)
  199.         {
  200.         oci->size = BUFSIZE;
  201.         oci->buffer = ibuf;
  202.         oci->bufptr = ibuf + oci->bufcnt - gotnow;
  203.         }
  204.     }
  205.     request = (xReq *)oci->bufptr;
  206.     if ((gotnow < sizeof(xReq)) ||
  207.         (gotnow < (needed = request_length(request, client))))
  208.     {
  209.         YieldControlNoInput();
  210.         return 0;
  211.     }
  212.     }
  213.     if (needed == 0)
  214.     needed = sizeof(xReq);
  215.     oci->lenLastReq = needed;
  216.  
  217.     /*
  218.      *  Check to see if client has at least one whole request in the
  219.      *  buffer.  If there is only a partial request, treat like buffer
  220.      *  is empty so that select() will be called again and other clients
  221.      *  can get into the queue.   
  222.      */
  223.  
  224.     if (gotnow >= needed + sizeof(xReq)) 
  225.     {
  226.     request = (xReq *)(oci->bufptr + needed);
  227.         if (gotnow >= needed + request_length(request, client))
  228.         BITSET(ClientsWithInput, fd);
  229.         else
  230.         YieldControlNoInput();
  231.     }
  232.     else
  233.     {
  234.     if (gotnow == needed)
  235.         AvailableInput = oc;
  236.     YieldControlNoInput();
  237.     }
  238.     if (++timesThisConnection >= MAX_TIMES_PER)
  239.     YieldControl();
  240.  
  241.     client->requestBuffer = (pointer)oci->bufptr;
  242.     return needed;
  243. }
  244.  
  245. /*****************************************************************
  246.  * InsertFakeRequest
  247.  *    Splice a consed up (possibly partial) request in as the next request.
  248.  *
  249.  **********************/
  250.  
  251. Bool
  252. InsertFakeRequest(client, data, count)
  253.     ClientPtr client;
  254.     char *data;
  255.     int count;
  256. {
  257.     OsCommPtr oc = (OsCommPtr)client->osPrivate;
  258.     register ConnectionInputPtr oci = oc->input;
  259.     int fd = oc->fd;
  260.     register xReq *request;
  261.     int gotnow, moveup;
  262.  
  263.     if (AvailableInput)
  264.     {
  265.     if (AvailableInput != oc)
  266.     {
  267.         AvailableInput->input->next = FreeInputs;
  268.         FreeInputs = AvailableInput->input;
  269.         AvailableInput->input = (ConnectionInputPtr)NULL;
  270.     }
  271.     AvailableInput = (OsCommPtr)NULL;
  272.     }
  273.     if (!oci)
  274.     {
  275.     if (oci = FreeInputs)
  276.         FreeInputs = oci->next;
  277.     else if (!(oci = AllocateInputBuffer()))
  278.         return FALSE;
  279.     oc->input = oci;
  280.     }
  281.     oci->bufptr += oci->lenLastReq;
  282.     oci->lenLastReq = 0;
  283.     gotnow = oci->bufcnt + oci->buffer - oci->bufptr;
  284.     if ((gotnow + count) > oci->size)
  285.     {
  286.     char *ibuf;
  287.  
  288.     ibuf = (char *)xrealloc(oci->buffer, gotnow + count);
  289.     if (!ibuf)
  290.         return(FALSE);
  291.     oci->size = gotnow + count;
  292.     oci->buffer = ibuf;
  293.     oci->bufptr = ibuf + oci->bufcnt - gotnow;
  294.     }
  295.     moveup = count - (oci->bufptr - oci->buffer);
  296.     if (moveup > 0)
  297.     {
  298.     if (gotnow > 0)
  299.         bcopy(oci->bufptr, oci->bufptr + moveup, gotnow);
  300.     oci->bufptr += moveup;
  301.     oci->bufcnt += moveup;
  302.     }
  303.     bcopy(data, oci->bufptr - count, count);
  304.     oci->bufptr -= count;
  305.     request = (xReq *)oci->bufptr;
  306.     gotnow += count;
  307.     if ((gotnow >= sizeof(xReq)) &&
  308.     (gotnow >= request_length(request, client)))
  309.     BITSET(ClientsWithInput, fd);
  310.     else
  311.     YieldControlNoInput();
  312.     return(TRUE);
  313. }
  314.  
  315. /*****************************************************************
  316.  * ResetRequestFromClient
  317.  *    Reset to reexecute the current request, and yield.
  318.  *
  319.  **********************/
  320.  
  321. ResetCurrentRequest(client)
  322.     ClientPtr client;
  323. {
  324.     OsCommPtr oc = (OsCommPtr)client->osPrivate;
  325.     register ConnectionInputPtr oci = oc->input;
  326.     int fd = oc->fd;
  327.     register xReq *request;
  328.     int gotnow;
  329.  
  330.     if (AvailableInput == oc)
  331.     AvailableInput = (OsCommPtr)NULL;
  332.     oci->lenLastReq = 0;
  333.     request = (xReq *)oci->bufptr;
  334.     gotnow = oci->bufcnt + oci->buffer - oci->bufptr;
  335.     if ((gotnow >= sizeof(xReq)) &&
  336.     (gotnow >= request_length(request, client)))
  337.     {
  338.     BITSET(ClientsWithInput, fd);
  339.     YieldControl();
  340.     }
  341.     else
  342.     YieldControlNoInput();
  343. }
  344.  
  345.     /* lookup table for adding padding bytes to data that is read from
  346.         or written to the X socket.  */
  347. static int padlength[4] = {0, 3, 2, 1};
  348.  
  349.  /********************
  350.  * FlushClient()
  351.  *    If the client isn't keeping up with us, then we try to continue
  352.  *    buffering the data and set the apropriate bit in ClientsWritable
  353.  *    (which is used by WaitFor in the select).  If the connection yields
  354.  *    a permanent error, or we can't allocate any more space, we then
  355.  *    close the connection.
  356.  *
  357.  **********************/
  358.  
  359. int
  360. FlushClient(who, oc, extraBuf, extraCount)
  361.     ClientPtr who;
  362.     OsCommPtr oc;
  363.     char *extraBuf;
  364.     int extraCount; /* do not modify... returned below */
  365. {
  366.     register ConnectionOutputPtr oco = oc->output;
  367.     int connection = oc->fd;
  368.     struct iovec iov[3];
  369.     char padBuffer[3];
  370.     long written;
  371.     long padsize;
  372.     long notWritten;
  373.     long todo;
  374.  
  375.     if (!oco)
  376.     return 0;
  377.     written = 0;
  378.     padsize = padlength[extraCount & 3];
  379.     notWritten = oco->count + extraCount + padsize;
  380.     todo = notWritten;
  381.     while (notWritten) {
  382.     long before = written;    /* amount of whole thing written */
  383.     long remain = todo;    /* amount to try this time, <= notWritten */
  384.     int i = 0;
  385.     long len;
  386.  
  387.     /* You could be very general here and have "in" and "out" iovecs
  388.      * and write a loop without using a macro, but what the heck.  This
  389.      * translates to:
  390.      *
  391.      *     how much of this piece is new?
  392.      *     if more new then we are trying this time, clamp
  393.      *     if nothing new
  394.      *         then bump down amount already written, for next piece
  395.      *         else put new stuff in iovec, will need all of next piece
  396.      *
  397.      * Note that todo had better be at least 1 or else we'll end up
  398.      * writing 0 iovecs.
  399.      */
  400. #define InsertIOV(pointer, length) \
  401.     len = (length) - before; \
  402.     if (len > remain) \
  403.         len = remain; \
  404.     if (len <= 0) { \
  405.         before = (-len); \
  406.     } else { \
  407.         iov[i].iov_len = len; \
  408.         iov[i].iov_base = (pointer) + before; \
  409.         i++; \
  410.         remain -= len; \
  411.         before = 0; \
  412.     }
  413.  
  414.     InsertIOV ((char *)oco->buf, oco->count)
  415.     InsertIOV (extraBuf, extraCount)
  416.     InsertIOV (padBuffer, padsize)
  417.  
  418.     errno = 0;
  419. #ifdef SPRITEPDEVCONN
  420.     if (PdevIsPdevConn(connection)) {
  421.         len = PdevWritev(connection, iov, i);
  422.     } else { 
  423.         len = writev(connection, iov, i);
  424.     }
  425.     if (len >= 0)
  426. #else
  427.     if ((len = writev(connection, iov, i)) >= 0)
  428. #endif
  429.     {
  430.         written += len;
  431.         notWritten -= len;
  432.         todo = notWritten;
  433.     }
  434. #ifdef apollo /* stupid sr10.1 UDS bug - supposedly fixed in sr10.2 */
  435.     else if ((errno == EWOULDBLOCK) && (todo > 4096))
  436.     {
  437.         todo = 4096;
  438.     }
  439. #endif
  440.     else if ((errno == EWOULDBLOCK)
  441. #ifdef SUNSYSV /* check for another brain-damaged OS bug */
  442.          || (errno == 0)
  443. #endif
  444. #ifdef EMSGSIZE /* check for another brain-damaged OS bug */
  445.          || ((errno == EMSGSIZE) && (todo == 1))
  446. #endif
  447.         )
  448.     {
  449.         /* If we've arrived here, then the client is stuffed to the gills
  450.            and not ready to accept more.  Make a note of it and buffer
  451.            the rest. */
  452.         BITSET(ClientsWriteBlocked, connection);
  453.         AnyClientsWriteBlocked = TRUE;
  454.  
  455.         if (written < oco->count)
  456.         {
  457.         if (written > 0)
  458.         {
  459.             oco->count -= written;
  460.             bcopy((char *)oco->buf + written,
  461.               (char *)oco->buf,
  462.               oco->count);
  463.             written = 0;
  464.         }
  465.         }
  466.         else
  467.         {
  468.         written -= oco->count;
  469.         oco->count = 0;
  470.         }
  471.  
  472.         if (notWritten > oco->size)
  473.         {
  474.         unsigned char *obuf;
  475.  
  476.         obuf = (unsigned char *)xrealloc(oco->buf,
  477.                          notWritten +
  478.                          OutputBufferSize);
  479.         if (!obuf)
  480.         {
  481.             close(connection);
  482.             MarkClientException(who);
  483.             oco->count = 0;
  484.             return(-1);
  485.         }
  486.         oco->size = notWritten + OutputBufferSize;
  487.         oco->buf = obuf;
  488.         }
  489.  
  490.         /* If the amount written extended into the padBuffer, then the
  491.            difference "extraCount - written" may be less than 0 */
  492.         if ((len = extraCount - written) > 0)
  493.         bcopy (extraBuf + written,
  494.                (char *)oco->buf + oco->count,
  495.                len);
  496.  
  497.         oco->count = notWritten; /* this will include the pad */
  498.         /* return only the amount explicitly requested */
  499.         return extraCount;
  500.     }
  501. #ifdef EMSGSIZE /* check for another brain-damaged OS bug */
  502.     else if (errno == EMSGSIZE)
  503.     {
  504.         todo >>= 1;
  505.     }
  506. #endif
  507.     else
  508.     {
  509. #ifdef SPRITEPDEVCONN
  510.         if (PdevIsPdevConn(connection)) {
  511.         PdevClose(connection);
  512.         } else
  513. #endif
  514.         close(connection);
  515.         MarkClientException(who);
  516.         oco->count = 0;
  517.         return(-1);
  518.     }
  519.     }
  520.  
  521.     /* everything was flushed out */
  522.     oco->count = 0;
  523.     /* check to see if this client was write blocked */
  524.     if (AnyClientsWriteBlocked)
  525.     {
  526.     BITCLEAR(ClientsWriteBlocked, oc->fd);
  527.     if (! ANYSET(ClientsWriteBlocked))
  528.         AnyClientsWriteBlocked = FALSE;
  529.     }
  530.     if (oco->size > OutputBufferSize)
  531.     {
  532.     unsigned char *obuf;
  533.  
  534.     obuf = (unsigned char *)xrealloc(oco->buf, OutputBufferSize);
  535.     if (obuf)
  536.     {
  537.         oco->size = OutputBufferSize;
  538.         oco->buf = obuf;
  539.     }
  540.     }
  541.     oco->next = FreeOutputs;
  542.     FreeOutputs = oco;
  543.     oc->output = (ConnectionOutputPtr)NULL;
  544.     return extraCount; /* return only the amount explicitly requested */
  545. }
  546.  
  547.  /********************
  548.  * FlushAllOutput()
  549.  *    Flush all clients with output.  However, if some client still
  550.  *    has input in the queue (more requests), then don't flush.  This
  551.  *    will prevent the output queue from being flushed every time around
  552.  *    the round robin queue.  Now, some say that it SHOULD be flushed
  553.  *    every time around, but...
  554.  *
  555.  **********************/
  556.  
  557. void
  558. FlushAllOutput()
  559. {
  560.     register int index, base, mask;
  561.     OsCommPtr oc;
  562.     register ClientPtr client;
  563.  
  564.     if (! NewOutputPending)
  565.     return;
  566.  
  567.     /*
  568.      * It may be that some client still has critical output pending,
  569.      * but he is not yet ready to receive it anyway, so we will
  570.      * simply wait for the select to tell us when he's ready to receive.
  571.      */
  572.     CriticalOutputPending = FALSE;
  573.     NewOutputPending = FALSE;
  574.  
  575.     for (base = 0; base < mskcnt; base++)
  576.     {
  577.     mask = OutputPending[ base ];
  578.     OutputPending[ base ] = 0;
  579.     while (mask)
  580.     {
  581.         index = ffs(mask) - 1;
  582.         mask &= ~lowbit(mask);
  583.         if ((index = ConnectionTranslation[(base << 5) + index]) == 0)
  584.         continue;
  585.         client = clients[index];
  586.         if (client->clientGone)
  587.         continue;
  588.         oc = (OsCommPtr)client->osPrivate;
  589.         if (GETBIT(ClientsWithInput, oc->fd))
  590.         {
  591.         BITSET(OutputPending, oc->fd); /* set the bit again */
  592.         NewOutputPending = TRUE;
  593.         }
  594.         else
  595.         (void)FlushClient(client, oc, (char *)NULL, 0);
  596.     }
  597.     }
  598.  
  599. }
  600.  
  601. void
  602. FlushIfCriticalOutputPending()
  603. {
  604.     if (CriticalOutputPending)
  605.     FlushAllOutput();
  606. }
  607.  
  608. void
  609. SetCriticalOutputPending()
  610. {
  611.     CriticalOutputPending = TRUE;
  612. }
  613.  
  614. /*****************
  615.  * WriteToClient
  616.  *    Copies buf into ClientPtr.buf if it fits (with padding), else
  617.  *    flushes ClientPtr.buf and buf to client.  As of this writing,
  618.  *    every use of WriteToClient is cast to void, and the result
  619.  *    is ignored.  Potentially, this could be used by requests
  620.  *    that are sending several chunks of data and want to break
  621.  *    out of a loop on error.  Thus, we will leave the type of
  622.  *    this routine as int.
  623.  *****************/
  624.  
  625. int
  626. WriteToClient (who, count, buf)
  627.     ClientPtr who;
  628.     char *buf;
  629.     int count;
  630. {
  631.     OsCommPtr oc = (OsCommPtr)who->osPrivate;
  632.     register ConnectionOutputPtr oco = oc->output;
  633.     int padBytes;
  634.  
  635.     if (!count)
  636.     return(0);
  637.  
  638.     if (!oco)
  639.     {
  640.     if (oco = FreeOutputs)
  641.     {
  642.         FreeOutputs = oco->next;
  643.     }
  644.     else if (!(oco = AllocateOutputBuffer()))
  645.     {
  646. #ifdef SPRITEPDEVCONN
  647.         if (PdevIsPdevConn(oc->fd)) {
  648.         PdevClose(oc->fd);
  649.         } else
  650. #endif
  651.         close(oc->fd);
  652.         MarkClientException(who);
  653.         return -1;
  654.     }
  655.     oc->output = oco;
  656.     }
  657.  
  658.     padBytes =  padlength[count & 3];
  659.  
  660.     if (oco->count + count + padBytes > oco->size)
  661.     {
  662.     BITCLEAR(OutputPending, oc->fd);
  663.     CriticalOutputPending = FALSE;
  664.     NewOutputPending = FALSE;
  665.     return FlushClient(who, oc, buf, count);
  666.     }
  667.  
  668.     NewOutputPending = TRUE;
  669.     BITSET(OutputPending, oc->fd);
  670.     bcopy(buf, (char *)oco->buf + oco->count, count);
  671.     oco->count += count + padBytes;
  672.     
  673.     return(count);
  674. }
  675.  
  676. static ConnectionInputPtr
  677. AllocateInputBuffer()
  678. {
  679.     register ConnectionInputPtr oci;
  680.  
  681.     oci = (ConnectionInputPtr)xalloc(sizeof(ConnectionInput));
  682.     if (!oci)
  683.     return (ConnectionInputPtr)NULL;
  684.     oci->buffer = (char *)xalloc(BUFSIZE);
  685.     if (!oci->buffer)
  686.     {
  687.     xfree(oci);
  688.     return (ConnectionInputPtr)NULL;
  689.     }
  690.     oci->size = BUFSIZE;
  691.     oci->bufptr = oci->buffer;
  692.     oci->bufcnt = 0;
  693.     oci->lenLastReq = 0;
  694.     return oci;
  695. }
  696.  
  697. static ConnectionOutputPtr
  698. AllocateOutputBuffer()
  699. {
  700.     register ConnectionOutputPtr oco;
  701.  
  702.     oco = (ConnectionOutputPtr)xalloc(sizeof(ConnectionOutput));
  703.     if (!oco)
  704.     return (ConnectionOutputPtr)NULL;
  705.     oco->buf = (unsigned char *) xalloc(OutputBufferSize);
  706.     if (!oco->buf)
  707.     {
  708.     xfree(oco);
  709.     return (ConnectionOutputPtr)NULL;
  710.     }
  711.     oco->size = OutputBufferSize;
  712.     oco->count = 0;
  713.     return oco;
  714. }
  715.  
  716. void
  717. FreeOsBuffers(oc)
  718.     OsCommPtr oc;
  719. {
  720.     register ConnectionInputPtr oci;
  721.     register ConnectionOutputPtr oco;
  722.  
  723.     if (AvailableInput == oc)
  724.     AvailableInput = (OsCommPtr)NULL;
  725.     if (oci = oc->input)
  726.     {
  727.     if (FreeInputs)
  728.     {
  729.         xfree(oci->buffer);
  730.         xfree(oci);
  731.     }
  732.     else
  733.     {
  734.         FreeInputs = oci;
  735.         oci->next = (ConnectionInputPtr)NULL;
  736.         oci->bufptr = oci->buffer;
  737.         oci->bufcnt = 0;
  738.         oci->lenLastReq = 0;
  739.     }
  740.     }
  741.     if (oco = oc->output)
  742.     {
  743.     if (FreeOutputs)
  744.     {
  745.         xfree(oco->buf);
  746.         xfree(oco);
  747.     }
  748.     else
  749.     {
  750.         FreeOutputs = oco;
  751.         oco->next = (ConnectionOutputPtr)NULL;
  752.         oco->count = 0;
  753.     }
  754.     }
  755. }
  756.  
  757. void
  758. ResetOsBuffers()
  759. {
  760.     register ConnectionInputPtr oci;
  761.     register ConnectionOutputPtr oco;
  762.  
  763.     while (oci = FreeInputs)
  764.     {
  765.     FreeInputs = oci->next;
  766.     xfree(oci->buffer);
  767.     xfree(oci);
  768.     }
  769.     while (oco = FreeOutputs)
  770.     {
  771.     FreeOutputs = oco->next;
  772.     xfree(oco->buf);
  773.     xfree(oco);
  774.     }
  775. }
  776. #ifdef SPRITEPDEVCONN
  777. /*
  778.  * This is needed until select() on a pdev returns when it is writable.
  779.  */
  780. PdevClearWriteBlockHack(fd)
  781.     int fd;
  782. {
  783.     if (AnyClientsWriteBlocked && GETBIT(ClientsWriteBlocked,fd))
  784.     {
  785.     NewOutputPending = TRUE;
  786.     BITSET(OutputPending, fd);
  787.     BITCLEAR(ClientsWriteBlocked, fd);
  788.     if (! ANYSET(ClientsWriteBlocked))
  789.         AnyClientsWriteBlocked = FALSE;
  790.     }
  791. }
  792. #endif
  793.